<?php
namespace Pw\MainBundle\Entity;

use Pw\MainBundle\Entity\Talent;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
class Role
{
	private $id;
	private $name;
	private $description;
	private $dateCreated;
	/////////////////////////////////
	private $iconName;

	private $icon;

	/**
	* @var Doctrine\Common\Collections\ArrayCollection
	*/
    private $heroes;

	public function __construct() {
		$this->heroes = new ArrayCollection();
	}

	public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
		$metadata->addConstraint(new UniqueEntity(array('fields' => 'name', 'groups' => array('add'))));
		$metadata->addPropertyConstraint('icon', new Assert\Image(array(
			'maxSize' => '1024k',
			'mimeTypesMessage' => 'Please upload a valid image (1MB max)',
        )));
		//Name
		$metadata->addPropertyConstraint('name', new Assert\NotBlank());
        $metadata->addPropertyConstraint('name', new Assert\Length(array("max" => 100)));
	}

	public function getArray4JSON(){
		$res = array();
		$res['name'] = $this->name;
		$res['iconName'] = $this->iconName;
		return $res;
	}

	public function getAbsolutePath($fileName){
        return $this->getUploadRootDir() . '/' . $fileName;
    }

    public function getWebPath($fileName){
        return $this->getUploadDir() . '/' . $fileName;
    }
	public function getDefaultImageSrc(){
		return $this->getWebPath('default.jpg');
	}

	public function getIconSrc(){
		if (!is_file($this->getAbsolutePath($this->iconName))) {
			return $this->getDefaultImageSrc();
		}
		return $this->getWebPath($this->iconName);
	}

    protected function getUploadRootDir(){
        return __DIR__ . '/../../../../web/' . $this->getUploadDir();
    }

    protected function getUploadDir(){
        return 'images/heroes';
    }
	//////////////////////////////////////
	public function doUpload(){
		if ($this->icon) {
			//try to delete existing file
			if ($this->iconName) {
				$this->doDelete($this->iconName);
			}
			$this->iconName = uniqid() . '.' . $this->icon->guessExtension();
			$this->icon->move($this->getUploadRootDir(), $this->iconName);
			unset($this->icon);			
		}
	}
	private function doDelete($fName){
		$file = $this->getAbsolutePath($fName);
		if (is_file($file)) {
			unlink($file);
		}
	}
	public function preRemove(){
		$this->filesToRemove[] = $this->iconName;
	}
	public function postRemove(){
		if (is_array($this->filesToRemove)) {
			foreach ($this->filesToRemove as $fileName) {
				$this->doDelete($fileName);
			}
		}
	}
	//////////////////////////////////////
	public function getIcon(){
		return $this->icon;
	}
	public function setIcon($icon){
		$this->icon = $icon;
	}

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Role
     */
    public function setName($name)
    {
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }
	
    public function setDescription($description)
    {
        $this->description = $description;
    
        return $this;
    }
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set iconName
     *
     * @param string $iconName
     * @return Role
     */
    public function setIconName($iconName)
    {
        $this->iconName = $iconName;
    
        return $this;
    }

    /**
     * Get iconName
     *
     * @return string 
     */
    public function getIconName()
    {
        return $this->iconName;
    }

    /**
     * Set dateCreated
     *
     * @param \DateTime $dateCreated
     * @return Role
     */
    public function setDateCreated($dateCreated)
    {
        $this->dateCreated = $dateCreated;
    
        return $this;
    }

    /**
     * Get dateCreated
     *
     * @return \DateTime 
     */
    public function getDateCreated()
    {
        return $this->dateCreated;
    }

    /**
     * Add heroes
     *
     * @param \Pw\MainBundle\Entity\Hero $heroes
     * @return Role
     */
    public function addHero(\Pw\MainBundle\Entity\Hero $heroes)
    {
        $this->heroes[] = $heroes;
    
        return $this;
    }

    /**
     * Remove heroes
     *
     * @param \Pw\MainBundle\Entity\Hero $heroes
     */
    public function removeHero(\Pw\MainBundle\Entity\Hero $heroes)
    {
        $this->heroes->removeElement($heroes);
    }

    /**
     * Get heroes
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getHeroes()
    {
        return $this->heroes;
    }

    /**
     * Add heroes
     *
     * @param \Pw\MainBundle\Entity\Hero $heroes
     * @return Role
     */
    public function addHeroe(\Pw\MainBundle\Entity\Hero $heroes)
    {
        $this->heroes[] = $heroes;
    
        return $this;
    }

    /**
     * Remove heroes
     *
     * @param \Pw\MainBundle\Entity\Hero $heroes
     */
    public function removeHeroe(\Pw\MainBundle\Entity\Hero $heroes)
    {
        $this->heroes->removeElement($heroes);
    }
}